package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.OrderInfo;
import com.haotunet.app.goldtreasure.model.TipsInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.MyAlertDialog;
import com.haotunet.app.goldtreasure.views.OrderPopupView;
import com.haotunet.app.goldtreasure.views.TipPopupView;
import com.haotunet.app.goldtreasure.views.TitleView;

public class RechargeFStebActivity extends BaseActivity {
    private View rootView;
    private TitleView mTitleView;
    private LinearLayout mTipLayout;
    private Button btnNext;
    private EditText mRechargeTxt;
    private String rechargeAmount;
    private OrderInfo orderInfo;
    private final int SUBMIT_RECHARGE_ORDER_SUCCESS = 1;
    private String tipTxt;
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case SUBMIT_RECHARGE_ORDER_SUCCESS:
                    orderInfo = (OrderInfo) msg.obj;
                    if ("true".equals(orderInfo.getStatus())) {
                        showRechargeFeeRemindDialog(orderInfo.getInfo());
                    } else {
                        if (shareStore.getInt(Strs.IS_HAD_BANK_CARD, 0) == 0) {
                            ActivityUtils.gotoNewUserBuy(RechargeFStebActivity.this, "2", orderInfo);
                        } else {
                            showOrderDetailView();
                        }
                    }
                    break;
                default:
                    break;
            }
        }

    };
    private ShareStore shareStore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_firststep);
        shareStore = new ShareStore(this);
        initView();
        getRechargeTips();
    }

    private void initView() {
        rootView = findViewById(R.id.root);
        mTitleView = (TitleView) findViewById(R.id.title_view);
        mTitleView.getTitletextView().setVisibility(View.VISIBLE);
        mTitleView.getTitletextView().setText("充值");
        mTitleView.getBackView().setVisibility(View.VISIBLE);
        mTitleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRechargeTxt = (EditText) findViewById(R.id.et_recharge_amount);
        mRechargeTxt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String inputString = s.toString();
                if (inputString.contains(".")) {
                    if (inputString.length() - 1 - inputString.toString().indexOf(".") > 2) {
                        inputString = (String) inputString.subSequence(0, inputString.indexOf(".") + 3);
                        mRechargeTxt.setText(inputString);
                        mRechargeTxt.setSelection(inputString.length());
                    }
                }
                if (inputString.trim().substring(0).equals(".")) {
                    inputString = "0";
                    mRechargeTxt.setText(inputString);
                    mRechargeTxt.setSelection(1);
                }
                if (inputString.startsWith("0") && inputString.trim().length() > 1) {
                    if (!inputString.substring(1, 2).equals(".")) {
                        mRechargeTxt.setText(inputString.subSequence(1, 2));
                        mRechargeTxt.setSelection(1);
                        return;
                    }
                }
            }
        });
        mTipLayout = (LinearLayout) findViewById(R.id.tip_layout);
        mTipLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new TipPopupView(RechargeFStebActivity.this,null, tipTxt, R.style.MyDialog).show();
            }
        });
        btnNext = (Button) findViewById(R.id.btn_next);
        btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                rechargeAmount = mRechargeTxt.getText().toString().trim();
                if (TextUtils.isEmpty(rechargeAmount)) {
                    UIHelper.showToast(RechargeFStebActivity.this, "请输入充值金额!");
                    return;
                }
                if (Double.parseDouble(rechargeAmount) < 2) {
                    UIHelper.showToast(RechargeFStebActivity.this, "起充金额为2元!");
                    return;
                }
                submitRechargeOrderInfo();
            }

            private void submitRechargeOrderInfo() {
                ActionUtil actionUtil = new ActionUtil(RechargeFStebActivity.this);
                actionUtil.submitRechargeorder(rechargeAmount);
                actionUtil.setOnPassBackListener(new OnPassBackListener() {

                    @Override
                    public void passBack(BaseModel model) {
                        if (isFinishing()){
                            return;
                        }
                        OrderInfo info = (OrderInfo) model;
                        if (info == null) {
                            UIHelper.showToast(RechargeFStebActivity.this, getString(R.string.rank_fail));
                            return;
                        }
                        if (info.getErrorMsg() != null) {
                            if (info.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                                ActivityUtils.forceQuit(RechargeFStebActivity.this,
                                        getString(R.string.crowding_in_tips_txt));
                            } else {
                                UIHelper.showToast(RechargeFStebActivity.this, info.getErrorMsg());
                            }
                        } else {
                            Message msg = new Message();
                            msg.what = SUBMIT_RECHARGE_ORDER_SUCCESS;
                            msg.obj = info;
                            handle.sendMessage(msg);
                        }
                    }
                });
            }
        });
    }

    private void getRechargeTips() {
        ActionUtil actionUtil = new ActionUtil(RechargeFStebActivity.this);
        actionUtil.getTips(1);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                TipsInfo tipInfo = (TipsInfo) model;
                if (tipInfo == null) {
                    UIHelper.showToast(RechargeFStebActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (tipInfo.getErrorMsg() != null) {
                    if (tipInfo.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(RechargeFStebActivity.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(RechargeFStebActivity.this, tipInfo.getErrorMsg());
                    }
                } else {
                    tipTxt = tipInfo.getTips();
                }
            }

        });

    }

    protected void showRechargeFeeRemindDialog(String info) {
        new MyAlertDialog(RechargeFStebActivity.this).builder().setTitle(getString(R.string.reminder)).setMsg(info)
                .setCancelable(false).setPositiveButton(getString(R.string.action_positive), true, new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (shareStore.getInt(Strs.IS_HAD_BANK_CARD, 0) == 0) {
                    ActivityUtils.gotoNewUserBuy(RechargeFStebActivity.this, "2", orderInfo);
                } else {
                    showOrderDetailView();
                }
            }
        }).setNegativeButton(getString(R.string.action_negative), new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    private void showOrderDetailView() {
        OrderPopupView orderPopupView = new OrderPopupView(RechargeFStebActivity.this, orderInfo.getPayment(), "2",
                orderInfo);
        orderPopupView.showAtLocation(rootView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
        ActivityUtils.backgroundAlpha(RechargeFStebActivity.this, 0.5f);
        orderPopupView.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ActivityUtils.backgroundAlpha(RechargeFStebActivity.this, 1f);
            }
        });
    }
}
